/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2011
Version 0.1
=======================================================*/
ConfigurationApplyWindow = function(configuration) {
	this.configuration = configuration;
	
	this.type = 0;
	
	this.groupList = new Ext.list.ListView({
		fieldLabel: 'Group List',
    	height: 300,
    	autoScroll: true,
    	multiSelect: true,
	 	hideHeaders: true,
	 	msgTarget: 'under',
    	style: 'background:#ffffff;',
		store: new Ext.data.ArrayStore({fields: ['Name','Group']}),
	    columns: [{header: 'Group List', dataIndex: 'Name'}]});

	this.deviceList = new Ext.list.ListView({
		fieldLabel: 'Device List',
    	height: 300,
    	autoScroll: true,
    	multiSelect: true,
	 	hideHeaders: true,
	 	msgTarget: 'under',
    	style: 'background:#ffffff;',
		store: new Ext.data.ArrayStore({fields: ['Name','Device']}),
	    columns: [{header: 'Device List',dataIndex: 'Name'}]});

    this.form = new Ext.FormPanel({
        labelAlign:'top',
        items: [this.groupList,this.deviceList],
        border: false,
        fileApply: true,
        bodyStyle:'background:transparent;padding:10px;'
    });

	ConfigurationApplyWindow.superclass.constructor.call(this, {
        title: 'Apply configuration to groups',
        iconCls: 'apply-configuration-icon',
        autoHeight: true,
        width: 400,
        resizable: false,
        plain:true,
        modal: true,
        y: 100,
        autoScroll: false,
        closeAction: 'hide',

        buttons:[{
            text: 'Apply',
            handler: this.onApply,
            scope: this
        },{
            text: 'Cancel',
            handler: this.hide.createDelegate(this, [])
        }],

        items: this.form
    });
}

Ext.extend(ConfigurationApplyWindow, Ext.Window, {
    show: function(type) {
    	this.type = type;
    	
    	if(type == 0) {
    		this.groupList.show();
    		this.deviceList.hide();
    		this.populateGroups();
    		this.setTitle('Apply configuration to groups');
    	} else {
    		this.groupList.hide();
    		this.deviceList.show();
    		this.populateDevices();
    		this.setTitle('Apply configuration to devices');
    	}

        ConfigurationWindow.superclass.show.apply(this, arguments);
    },
    
    populateGroups : function(){
		SGui.getGroups(this.cbPopulateGroups,this);
	},

	cbPopulateGroups : function(obj,result){
		obj.groupList.store.removeAll();
		if(result)
		{
			if(result instanceof Array) {			
				for(var i=0; i<result.length; i++){
					var group = new Group(result[i]);
					obj.groupList.store.loadData([[group.name,group]],true);
				}
			} else {
				var group = new Group(result);
				obj.groupList.store.loadData([[group.name,group]],true);
			}
		}
	},    
    
    populateDevices : function(){
		SGui.getDevices(this.cbPopulateDevices,this);
	},

	cbPopulateDevices : function(obj,result){
		obj.deviceList.store.removeAll();
		if(result)
		{
			if(result instanceof Array) {			
				for(var i=0; i<result.length; i++){
					var device = new Device(result[i]);
					if(device.configID != obj.configuration.id)
						obj.deviceList.store.loadData([[device.name,device]],true);
				}
			} else {
				var device = new Device(result);
				if(device.configID != obj.configuration.id)
					obj.deviceList.store.loadData([[device.name,device]],true);
			}
		}
	},

    onApply: function() {
    	if(this.type == 0) {    		
    		var groups = new Array();
    		var records = this.groupList.getSelectedRecords();
    		for(var i=0; i<records.length; i++) {
    			groups.push(records[i].get('Group').id);
    		}
    		
    		if(groups.length == 0) {
    			this.groupList.markInvalid("You must have selected at least one group!");
    			return;
    		}

    		this.el.mask('Applying Configuration...', 'x-mask-loading');
			SGui.applyConfigurationToGroups(this.configuration.id,groups,this.csApply,this);
    	} else {
    		var devices = new Array();
    		var records = this.deviceList.getSelectedRecords();
    		for(var i=0; i<records.length; i++) {
    			devices.push(records[i].get('Device').id);
    		}
    		
    		if(devices.length == 0) {
    			this.deviceList.markInvalid("You must have selected at least one group!");
    			return;
    		}

    		this.el.mask('Applying Configuration...', 'x-mask-loading');
			SGui.applyConfigurationToDevices(this.configuration.id,devices,this.csApply,this);
    	}
    },

    csApply: function(obj,result) {
		obj.el.unmask();

    	if(result instanceof Array && result[0] == '-1') {
    		Ext.MessageBox.show({title: 'Error', msg: 'Invalid Configuration.', buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.ERROR});
    	} else {
		    obj.hide();
			obj.fireEvent('apply');
    	}
    }
});
